package com.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.authc.credential.PasswordService;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.*;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by liam on 16/10/10.
 */
public class HelloShiro {
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloShiro.class);

    public static void main(String[] args) {
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        Subject subject = SecurityUtils.getSubject();

//        PasswordService passwordService =  new DefaultPasswordService();
//        String encryptedPassword = passwordService.encryptPassword("liampassword");

        UsernamePasswordToken token = new UsernamePasswordToken("shiro","liam");

        try {
            subject.login(token);
        }catch (AuthenticationException e){
            LOGGER.info("登入失败");
        }
        LOGGER.info("登入成功!" + subject.getPrincipal());
        subject.logout();;
    }
}
